<template>
  <div>
    <el-container>
      <el-aside :style="{ width: isCollapse ? 'auto' : '250px' }">
        <navbar :menus="menus" :collapse="isCollapse"></navbar>
      </el-aside>
      <el-container>
        <el-header>
          <i
            class="el-collapse el-icon-s-fold"
            :class="isCollapse ? 'collapseTransform' : ''"
            @click="handleCollapse"
          >
          
          </i>
          <el-dropdown>
            <span class="el-dropdown-link">
            {{user.username}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
             
              <el-dropdown-item @click.native="handleLogout">
                注销
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
        <el-footer>
          Copyright © 2021 - 2021 9I Co. All Rights Reserved. 就爱科技 版权所有
        </el-footer>
      </el-container>
    </el-container>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>真的要注销当前用户吗？请确保当前工作已经保存^_^</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmLogout">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import routes from '../router/routes'
import Navbar from '../components/Navbar'
import { getTokenData } from '../utils/auth'
export default {
  components: {
    Navbar,
  },
  data() {
    return {
      isCollapse: false,
      dialogVisible: false,
      user:{
        username:""
      }
      
    }
  },
   mounted () {
    const data = getTokenData()
    console.log(data);
    console.log(data.username);
    console.log(this.user.username);
    this.user.username =
      data.nickname && data.nickname.trim().length > 0 ? data.nickname : data.username
    console.log(data)
  },
 
  methods: {
    resolve(arr, parentPath = '') {
      let newArr = []
      //第一步遍历数组当中的每一个元素
      arr.forEach((route) => {
        let obj = Object.assign({}, route)

        // 当上级路径存在，并且最后一位不为/，则后面添加 /
        parentPath =
          parentPath && parentPath.lastIndexOf('/') !== parentPath.length - 1
            ? parentPath + '/'
            : parentPath

        // 总是给加上父级路径
        obj.path = parentPath + obj.path
        // 第二步 判断有没有children属性 如有，那么需要再处理，如果没有则直接push到一个新数组中
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolve(obj.children, obj.path)
        }
        // 如果当前路由元信息有hidden信息，那么就将其子节点提升上来
        if (obj.meta && obj.meta.hidden) {
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item)
              newArr.push(tmp)
            })
          }
        } else {
          newArr.push(obj)
        }
      })

      return newArr
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    handleLogout() {
      console.log(17173)
      this.dialogVisible = true
    },
    handleConfirmLogout() {
      this.dialogVisible = false
      localStorage.removeItem('accessToken')
      localStorage.removeItem('refreshToken')
      this.$router.push('/login')
    },
  },
  computed: {
    menus() {
      let arr = this.resolve(routes)

      return arr
    },
    
  },
}
</script>

<style scoped>
.el-header {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-aside {
  height: 1674px;
  background-color: rgb(84, 92, 100);
}
.el-main {
  min-height: calc(100vh - 120px);
}
.el-footer {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}
.el-collapse {
  font-size: 2rem;
}
.collapseTransform {
  transform: rotate(-90deg);
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
